package info.motteke.qjdbc.column;

import info.motteke.qjdbc.column.filter.BetweenFilter;
import info.motteke.qjdbc.column.filter.EqualFilter;
import info.motteke.qjdbc.column.filter.GreaterFilter;
import info.motteke.qjdbc.column.filter.GreaterOrEqualFilter;
import info.motteke.qjdbc.column.filter.InFilter;
import info.motteke.qjdbc.column.filter.LessFilter;
import info.motteke.qjdbc.column.filter.LessOrEqualFilter;
import info.motteke.qjdbc.column.variable.StringVariable;
import info.motteke.qjdbc.exception.SqlException;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 文字列を表すメタデータです。
 *
 * @author YWak
 *
 */
public final class StringMeta extends Meta {

    public StringMeta(String name) {
        super(name);
    }

    public EqualFilter equal(String value) {
        return equal(new StringVariable(value));
    }

    public EqualFilter equal(StringVariable var) {
        return new EqualFilter(var);
    }

    public GreaterFilter greaterThan(String value) {
        return greaterThan(new StringVariable(value));
    }

    public GreaterFilter greaterThan(StringVariable var) {
        return new GreaterFilter(var);
    }

    public GreaterOrEqualFilter greaterThanOrEqual(String value) {
        return greaterThanOrEqual(new StringVariable(value));
    }

    public GreaterOrEqualFilter greaterThanOrEqual(StringVariable var) {
        return new GreaterOrEqualFilter(var);
    }

    public LessFilter lessThan(String value) {
        return lessThan(new StringVariable(value));
    }

    public LessFilter lessThan(StringVariable var) {
        return new LessFilter(var);
    }

    public LessOrEqualFilter lessThanOrEqual(String value) {
        return lessThanOrEqual(new StringVariable(value));
    }

    public LessOrEqualFilter lessThanOrEqual(StringVariable var) {
        return new LessOrEqualFilter(var);
    }

    public BetweenFilter between(String start, String end) {
        return between(new StringVariable(start), new StringVariable(end));
    }

    public BetweenFilter between(StringVariable start, StringVariable end) {
        return new BetweenFilter(start, end);
    }

    public InFilter in(String... values) {
        StringVariable[] vars = new StringVariable[values.length];

        for (int i = 0; i < values.length; i++) {
            vars[i] = new StringVariable(values[i]);
        }

        return new InFilter(vars);
    }

    String getValue(ResultSet rs) {
        try {
            return rs.getString(getName());
        } catch (SQLException e) {
            throw new SqlException(e);
        }
    }
}
